#include <string.h>
#include "server.h"
#include "sound.h"
#include "filedialog.h"

void ConnectServer()//ok连接服务器
{
    int len,result;
    sockfd = socket(AF_INET,SOCK_STREAM,0);
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr("127.0.0.1");
    address.sin_port = 9114;
    len = sizeof(address);
    result = connect(sockfd,(struct sockaddr *)&address,len);
    if(result == -1)
        connect_code = FALSE;
    else
        connect_code = TRUE;
}

void c_parse(struct Package *buff)//解析所有来自服务器的包，并作出响应
{
    switch((*buff).type){
    case 1:{
        char s[30] = {'\0'};
        sprintf(s,"帐号密码均为：%d",(*buff).destination);
        gdk_threads_enter();
        gtk_label_set_label(GTK_LABEL(lb_info),s);
        gtk_widget_show_all(GTK_WIDGET(info_window));
        gdk_threads_leave();
        break;
    }
    case -1:{
        gdk_threads_enter();
        gtk_label_set_label(GTK_LABEL(lb_info),"申请失败");
        gtk_widget_show_all(GTK_WIDGET(info_window));
        gdk_threads_leave();
        break;
    }
    case 2:{
        /*********************
         *buff->msg :个人签名，或作为登录者的id号，因为label设置为char *
         *buff->destination: 个人id
         *********************/
        struct Package pkg;
        char id[15]={'\0'};
        gdk_threads_enter();

        gtk_widget_hide_all(GTK_WIDGET(login_window));
        gtk_widget_show_all(GTK_WIDGET(main_window));
        sprintf(id,"%d",buff->destination);
        gtk_label_set_label(GTK_LABEL(self_label),id);
        //gtk_label_set_label(GTK_LABEL(self_label),buff->destination);

        gdk_threads_leave();
        {
            pkg.type = 7;
        }
        write(sockfd,&pkg,sizeof(pkg));//请求好友列表
        break;
    }
    case -2:{
        gdk_threads_enter();
        gtk_label_set_label(GTK_LABEL(lb_info),"用户名或密码错误！");
        gtk_widget_show_all(GTK_WIDGET(info_window));
        gdk_threads_leave();
        break;
    }
    case 3:{
        printf("case 3\n");
        break;
    }
    case -3:{
        printf("case -3\n");
        break;
        //gtk_widget_is_sensitive()
        //gtk_window_
    }
    case 4:{//接收消息,创建线程，添加线程、窗口、id映射
        if(id_to_window(&map,buff->destination) != NULL){

            //gtk_widget_show_all(GTK_WIDGET(find_window(&thread_id_window,buff->destination)));
            gdk_threads_enter();
            AddMessage(buff->destination,buff->msg);
            gdk_threads_leave();//小心此处没有解锁就会导致ui线程锁住

            g_thread_create(sound,NULL,FALSE,NULL);

            sem_wait(&sign_lb_lock);
            gtk_label_set_label(GTK_LABEL(id_to_idnum(&map,buff->destination)),"*");
            sem_post(&sign_lb_lock);
            gdk_threads_enter();
            gtk_widget_show_all(GTK_WIDGET(main_window));
            gdk_threads_leave();

            break;
        }
        gdk_threads_enter();
        GtkWidget * window;
        GtkWidget * text_view = id_to_idlabel(&map,buff->destination);//返回的是消息接收框，上框
        ChatWindow(&window,&text_view);

        GThread * thread;

        add_map_again(&map,buff->destination,window,text_view,thread);//之前担心的问题在这里确实出现了，即线程已经创建，但是映射还没到位
        AddMessage(buff->destination,buff->msg);
        gdk_threads_leave();
        g_thread_create(sound,NULL,FALSE,NULL);//发出信息提示
        sem_wait(&sign_lb_lock);
        gtk_label_set_label(GTK_LABEL(id_to_idnum(&map,buff->destination)),"*");
        sem_post(&sign_lb_lock);
        gdk_threads_enter();
        gtk_widget_show_all(GTK_WIDGET(main_window));
        gdk_threads_leave();
        break;
    }
    case -4:{
        printf("-4");

        break;
    }
    case 5:{
        char s[20]={'\0'};
        sprintf(s,"%d",buff->destination);
        gdk_threads_enter();
        gtk_label_set_label(GTK_LABEL(lb_info_sour),s);
        gtk_widget_show_all(GTK_WIDGET(confirm_window));
        g_thread_create(sound,NULL,FALSE,NULL);//发出信息提示
        gdk_threads_leave();

        break;
    }
    case 6:{
        printf("6");

        printf("删除成功!\n");
        break;
    }
    case -6:{
        printf("-6");

        printf("已无此联系人\n");
        break;
    }
    case 7:{//输出好友，添加id、idlabel、id信息显示数字
        char s[20] = {'\0'};
        sprintf(s,"%d",buff->destination);

        gdk_threads_enter();
        GtkWidget * lb_id;
        if(strcmp(buff->msg,"null") == 0)
            lb_id = gtk_label_new(s);
        else
            lb_id = gtk_label_new(buff->msg);
        GtkWidget * lb_msg = gtk_label_new("");
        GtkWidget * event_box = gtk_event_box_new();
        GtkWidget * align = gtk_alignment_new(0,0,0,0);
        GtkWidget * hbox = gtk_hbox_new(FALSE,10);
        FriendId * friendbox = (FriendId *)malloc( sizeof(FriendId) );
        friendbox->lb_id = lb_id;//这里记录的是地址信息，不是值，若获得值请用get方法
        friendbox->lb_msg = lb_msg;
        //add_id(&id_map,lb_id,buff->destination,lb_msg);//添加字典映射
        add_map_first(&map,buff->destination,lb_id,lb_msg);
        gtk_container_add(GTK_CONTAINER(event_box),lb_id);
        gtk_container_add(GTK_CONTAINER(align),event_box);
        gtk_box_pack_start(GTK_BOX(hbox),align,FALSE,FALSE,0);
        gtk_box_pack_start(GTK_BOX(hbox),lb_msg,FALSE,FALSE,0);
        gtk_box_pack_start(GTK_BOX(friend_vbox),hbox,FALSE,FALSE,0);
        g_signal_connect((gpointer)event_box,"button_press_event",G_CALLBACK(creat_chat_event),friendbox);

        gtk_widget_show_all(GTK_WIDGET(main_window));
        gdk_threads_leave();
        break;
    }
    case 8:
    {
        printf("8");
        printf("添加成功\n");
        break;
    }
    case -8:
    {
        printf("-8");
        printf("添加失败\n");
        break;
    }
    case 10:
    {
        printf("接收文件提醒\n");
        gdk_threads_enter();
        init_file_dialog(buff->sock,buff->other);
        gtk_widget_show_all(GTK_WIDGET(file_dialog));
        gdk_threads_leave();
        break;
    }
    case 11:
    {
        printf("他已经同意啦\n");
        printf("Id:%d\nfile:%s\nip:%s",buff->destination,buff->other,buff->msg);
        struct Package * temp = (struct Package *)malloc(sizeof(struct Package));
        memcpy(temp, buff,sizeof(struct Package));
        printf("测试文件名称%s\n",temp->other);
        g_thread_create(thread_send_file,temp,FALSE,NULL);

        break;
    }
    default:
        printf("未知指令\n");
    }
}
//void * c_handle_parse()//从队列中取包，并传送给解析函数，状态图保证每个解析是否可以终结的
void *c_handle_parse()//ok
{
    struct Package buff;
    for(;;){
        if(!empty_msg(&cqueue)){
            ret_dele_pkg(&cqueue,&buff);
            c_parse(&buff);
        }
        usleep(500000);//微妙级别
    }
}
void * c_handle_request()//ok包接收线程
{
    struct Package buff;
    for(;exit_code != TRUE;){
        read(sockfd,&buff,sizeof(buff));
        add_pkg(&cqueue,&buff);
    }
}
/*printf("5");
char s[20];
pid_t pid;
int pipe_fd;
int open_mode = O_RDONLY;
struct Package pkg;
char apply_buffer[PIPE_BUF+1]={'\0'};
sprintf(s,"%d",buff->destination);
pid = fork();
switch(pid){
case -1:
    printf("fork error\n");
    break;
case 0:
    execlp("./apply","Apply",s,0);
    break;
default:
    pipe_fd = open("/tmp/my_fifo",open_mode);
    if(pipe_fd != -1){
        read(pipe_fd,apply_buffer,PIPE_BUF);
        if(strcmp(apply_buffer,"ok") == 0){
            printf("\n您已同意好友请求\n");
            pkg.type = 8;
            pkg.destination = buff->destination;
            write(sockfd,&pkg,sizeof(pkg));
        }
        close(pipe_fd);
    }
}*/        //if(( thread = g_thread_create(chat_msg_handle,data,FALSE,NULL)) == NULL){
//    printf("deadliness error!");
//   exit(0);
// }
//gtk_window_set_title(GTK_WINDOW(window),"聊天窗口");
//gtk_widget_show_all(GTK_WIDGET(window));
//AddMessage(buff->destination,buff->msg);
//gdk_threads_leave();
//add_thread(&thread_id_window,thread,buff->destination,window);
//FriendId * data = (FriendId *)malloc(sizeof(FriendId));
//data->lb_id = find_lb_id(&id_map,buff->destination);
//data->lb_id = id_to_idlabel(&map,buff->destination);
//data->lb_msg = find_lb_msg(&id_map,buff->destination);
//data->lb_msg = id_to_idnum(&map,buff->destination);
// data->text_view = text_view;
